package com.bh.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bh.common.basemapper.UpdateBatchWrapper;
import com.bh.common.enums.OrderStatus;
import com.bh.common.constant.Symbol;
import com.bh.common.exception.ServiceException;
import com.bh.common.utils.DateUtils;
import com.bh.common.utils.SecurityUtils;
import com.bh.common.utils.spring.SpringUtils;
import com.bh.order.domain.JcOrder;
import com.bh.order.domain.JcOrderBack;
import com.bh.order.domain.JcOrderBackImages;
import com.bh.order.domain.JcOrderBackImagesVo;
import com.bh.order.mapper.JcOrderBackMapper;
import com.bh.order.mapper.JcOrderMapper;
import com.bh.order.service.IJcOrderBackImagesService;
import com.bh.order.service.IJcOrderBackService;
import com.bh.order.vo.JcBackOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 运单回单Service业务层处理
 *
 * @author slx
 * @date 2023-04-22
 */

@Service
public class JcOrderBackServiceImpl extends ServiceImpl<JcOrderBackMapper, JcOrderBack> implements IJcOrderBackService {
    @Autowired
    private JcOrderBackMapper jcOrderBackMapper;

    @Autowired
    private JcOrderMapper jcOrderMapper;

    @Autowired
    private IJcOrderBackImagesService jcOrderBackImagesService;

    /**
     * 查询运单回单
     *
     * @param id 运单回单主键
     * @return 运单回单
     */
    @Override
    public JcOrderBack selectJcOrderBackById(Long id) {
        return jcOrderBackMapper.selectById(id);
    }

    /**
     * 查询运单回单列表
     *
     * @param jcOrderBack 运单回单
     * @return 运单回单
     */
    @Override
    public List<JcOrderBack> selectJcOrderBackList(JcOrderBack jcOrderBack) {
        return jcOrderBackMapper.selectJcOrderBackList(jcOrderBack);
    }

    @Override
    public List<JcBackOrderVO> selectJcBackOrderList(JcBackOrderVO backOrderVO) {
        List<JcBackOrderVO> backOrderVOS = jcOrderBackMapper.selectYqsJcOrderBackList(backOrderVO);
        backOrderVOS.forEach(item -> {
            if (item.getIsUpload() == 1) {
                List<JcOrderBackImages> images = jcOrderBackImagesService.selectJcOrderBackImagesListByBackId(item.getBackId());
                item.setImages(images);
            }
        });
        return backOrderVOS;
    }

    @Override
    public List<JcOrderBack> selectJcOrderBackListByOrderId(Long orderId) {
        return jcOrderBackMapper.selectJcOrderBackListByOrderId(orderId);
    }


    @Override
    public int receive(Long[] ids) {
        List<JcOrderBack> list = this.listByIds(Arrays.asList(ids));
        List<JcOrder> orderList = new ArrayList<>();
        list.forEach(item -> {
            if (item.getStatus() != OrderStatus.NOT_SING.getValue()) {
                throw new ServiceException("只能操作未接收状态的单据");
            } else {
                item.setStatus(OrderStatus.NOT_SING.getValue());
                JcOrder order = new JcOrder();
                order.setId(item.getJcOrderId());
                order.setStatus(OrderStatus.END_SING.getValue());
                orderList.add(order);
            }
        });
        if (!orderList.isEmpty()) {
            jcOrderMapper.updateBatch(orderList, new UpdateBatchWrapper<JcOrder>().setUpdateFields(JcOrder::getStatus));
        }
        return SpringUtils.getBean(this.getClass()).updateBatchById(list) ? 1 : 0;
    }

    @Override
    public int submit(Long[] ids) {
        List<JcOrderBack> list = this.listByIds(Arrays.asList(ids));
        List<JcOrder> orderList = new ArrayList<>();
        list.forEach(item -> {
            if (item.getStatus() != 15) {
                throw new ServiceException("只能操作接收状态的单据");
            } else {
                item.setStatus(16);
                JcOrder order = new JcOrder();
                order.setId(item.getJcOrderId());
                order.setStatus(16);
                orderList.add(order);
            }
        });
        if (!orderList.isEmpty()) {
            jcOrderMapper.updateBatch(orderList, new UpdateBatchWrapper<JcOrder>().setUpdateFields(
                    JcOrder::getStatus
            ));
        }
        return SpringUtils.getBean(this.getClass()).updateBatchById(list) ? 1 : 0;
    }

    @Override
    public int uploadBackImage(JcOrderBackImagesVo jcOrderBackImagesVo) {
        List<JcOrderBackImages> list = new ArrayList<>();
        String[] images = jcOrderBackImagesVo.getImages().split(Symbol.COMMA);
        for (String imageUrl : images) {
            // 上传并返回新文件名称
            JcOrderBackImages orderBackImages = new JcOrderBackImages();
            orderBackImages.setUrl(imageUrl);
            String name = imageUrl.substring(imageUrl.lastIndexOf("/") + 1, imageUrl.lastIndexOf("_"));
            orderBackImages.setName(name);
            orderBackImages.setJcOrderBackId(jcOrderBackImagesVo.getJcOrderBackId());
            list.add(orderBackImages);
        }
        jcOrderBackImagesService.saveBatch(list);
        JcOrderBack jcOrderBack = new JcOrderBack();
        jcOrderBack.setId(jcOrderBackImagesVo.getJcOrderBackId());
        jcOrderBack.setIsUpload(1);
        return updateById(jcOrderBack) ? 1 : 0;
    }

    /**
     * 新增运单回单
     *
     * @param jcOrderBack 运单回单
     * @return 结果
     */
    @Override
    public int insertJcOrderBack(JcOrderBack jcOrderBack) {
        jcOrderBack.setCreateName(SecurityUtils.getUsername());
        jcOrderBack.setCreateTime(DateUtils.getNowDate());
        return jcOrderBackMapper.insert(jcOrderBack);
    }

    /**
     * 修改运单回单
     *
     * @param jcOrderBack 运单回单
     * @return 结果
     */
    @Override
    public int updateJcOrderBack(JcOrderBack jcOrderBack) {
        return jcOrderBackMapper.updateById(jcOrderBack);
    }

    /**
     * 批量删除运单回单
     *
     * @param ids 需要删除的运单回单主键
     * @return 结果
     */
    @Override
    public int deleteJcOrderBackByIds(Long[] ids) {
        return jcOrderBackMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除运单回单信息
     *
     * @param id 运单回单主键
     * @return 结果
     */
    @Override
    public int deleteJcOrderBackById(Long id) {
        return jcOrderBackMapper.deleteById(id);
    }
}
